const { Base } = require('../utils');

module.exports = class extends Base {
  async prompting() {
    this.answers = await this.prompt([
      {
        type: 'input',
        name: 'project_name',
        message: 'Your project name',
        default: 'scrapy_demo',
        validate: project_name => {
          if (project_name && Base.existsPath(Base.getAbsolutePath(process.cwd(), project_name))) {
            return `The project "${project_name}" has already existed.`;
          }
          return Base.validationName(project_name);
        },
      },
      {
        type: 'confirm',
        name: 'robotsObey',
        message: 'Obey robots.txt',
        default: false
      },
      {
        type: 'checkbox',
        name: 'projectConfigPipeline',
        message: 'Project Config - Pipeline',
        loop: false,
        choices: [
          {
            name: 'MongoStatPipeline',
            value: 'MongoStatPipeline',
            checked: true,
          },
        ],
      },
      {
        type: 'checkbox',
        name: 'projectConfigMiddleware',
        message: 'Project Config - Middleware',
        loop: false,
        choices: [
          {
            name: 'RandomUserAgentMiddleware',
            value: 'RandomUserAgentMiddleware',
            checked: true,
          },
          {
            name: 'ThrottlingMiddleware',
            value: 'ThrottlingMiddleware',
            checked: false,
          },
        ],
      },
      {
        type: 'checkbox',
        name: 'projectConfigExtension',
        message: 'Project Config - Extension',
        loop: false,
        choices: [
          {
            name: 'TimeStats',
            value: 'TimeStats',
            checked: true,
          },
          {
            name: 'ServerInfo',
            value: 'ServerInfo',
            checked: true,
          },
          {
            name: 'MyStatsMailer',
            value: 'MyStatsMailer',
            checked: false,
          },
        ],
      },
      {
        type: 'input',
        name: 'myStatsMailerExtensionEmails',
        message: 'Extension MyStatsMailer: Emails (Multiple email are separated by commas.)',
        validate: Base.validationEmail,
        when: (res) => res.projectConfigExtension.includes('MyStatsMailer'),
      },
    ]);

    this.answers.projectName = Base.stringCamelcase(this.answers.project_name);
  }

  // 复制模板文件, 复制整个模板目录的文件, 后期拓展直接在代码模板里增删改即可
  writing() {
    this.projectDir = this.destinationPath(this.answers.project_name);
    this.moduleDir = this.destinationPath(this.projectDir, Base.MODULE);

    // 复制项目模板文件
    Base.TMPL_PROJECT_MODULE_FILES.forEach(file => {
      // 1. 将模板中的 module 目录重命名为 project_name
      const to = Base.renameTmplFileExt(this.destinationPath(this.projectDir, this.answers.project_name, file.replace(Base.TMPL_PROJECT_MODULE_ROOT, '')));
      this.fs.copyTpl(file, to, this.answers);
    });

    // 2. 复制除了 module 目录的其他文件
    Base.TMPL_PROJECT_OTHER_FILES.forEach(file => {
      const to = Base.renameTmplFileExt(this.destinationPath(this.projectDir, file.replace(Base.TMPL_PROJECT_ROOT, '')));
      this.fs.copyTpl(file, to, this.answers);
    });
  }

  end() {
    this.log();
    this.log(`You can start your first spider with:`);
    this.log(`    cd ${this.answers.project_name}`);
    this.log(`    yo scrapy:genspider`);
  }
}